/**
 * Created with JetBrains WebStorm.
 * User: absir
 * Date: 14-11-7
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
var auiModule = angular.module('aui', ['ui.bootstrap']);

/*
 * aui configure
 */
auiModule.tplDir = 'aui/tpl/';
auiModule.tplCached = false;
auiModule.tplSrc = function (src, attr) {
    if (attr) {
        if (attr.charAt(0) == ':') {
            src = attr.substr(1);

        } else {
            return attr;
        }
    }

    return auiModule.tplDir + src;
};

/*
 * aui init function
 */
auiModule.run(function ($injector, $rootScope, $compile, $http, $templateRequest, $templateCache, $sce) {

    /*
     * init i18n language
     */
    $.i18n.properties({
        name: 'strings',
        path: auiModule.tplDir + 'i18n/',
        mode: 'both',
        language: auiModule.language || $.i18n.browserLang(),
        cache: false,
        encoding: 'UTF-8'
        //callback: function () {}
    });

    auiModule.$rootScope = $rootScope;
    auiModule.$compile = $compile;
    auiModule.$http = $http;
    auiModule.$sce = $sce;

    var tplCaches = {};
    auiModule.thenTemplate = function (then, src) {
        var template = tplCaches[src];
        if (template) {
            then(template);

        } else {
            $templateRequest(src, true).then(function (template) {
                tplCaches[src] = template;
                if (auiModule.tplCached) {
                    $templateCache.put(src, template);
                }

                then(template);
            });
        }
    };

    auiModule.removeTemplate = function (src) {
        tplCaches.remove(src);
        if (auiModule.tplCached) {
            $templateCache.remove(src);
        }
    }

    auiModule.createComponent = function (then, scope, element, src) {
        auiModule.thenTemplate(function (template) {
            $element = $(element);
            $element.after(template);
            $next = $element.next();
            $compile($next.contents())(scope);
            then($next);
        }, src);
    };

    var $document = $(document);
    auiModule.$document = $document;
    var $auiElement = $document.children().last();
    var $auiElement = $auiElement.append('<auiElement><auiPre/></auiElement>').children().last().children().first();
    auiModule.$auiElement = $auiElement;
    var instanceComponents = {};
    auiModule.instanceComponent = function (then, src) {
        var components = instanceComponents[src];
        if (components) {
            then(components[0], components[1]);

        } else {
            var scope = $rootScope.$new();
            auiModule.createComponent(function ($element) {
                components = [$element, scope];
                instanceComponents[src] = components;
                then($element, scope);
            }, scope, $auiElement, src, $templateRequest, $templateCache, $compile);
        }
    };
});

/*
 * auiModule core  directive
 */
auiModule.directive("aScope", function () {
    return {
        restrict: "EA",
        scope: true,
        priority: 450,
        compile: function () {
            return {
                pre: function (scope, element, attrs) {
                    scope.$eval(attrs.aScope);
                }
            };
        }
    };
});

auiModule.directive("aWidth", function ($window) {
    return {
        restrict: "A",
        link: function (scope, element, attrs) {
            $this = $(element);
            $this.css("overflow", "auto");
            $parent = $this.parent();
            var resizing = false;

            function resize() {
                if (!resizing) {
                    resizing = true;
                    $this.width($parent.width() - attrs.aWidth);
                    resizing = false;
                }
            };

            $($window).resize(resize);
            $parent.resize(resize);
            resize();
        }
    };
});

auiModule.directive("aHeight", function ($window) {
    return {
        restrict: "A",
        priority: 450,
        link: function (scope, element, attrs) {
            $this = $(element);
            $this.css("overflow", "auto");
            $parent = $this.parent();
            var resizing = false;

            function resize() {
                if (!resizing) {
                    resizing = true;
                    $this.height($parent.height() - attrs.aHeight);
                    resizing = false;
                }
            };

            $($window).resize(resize);
            $parent.resize(resize);
            resize();
        }
    };
});

auiModule.directive("aTitle", function () {
    return {
        restrict: "A",
        priority: 65535,
        link: function (scope, element, attrs) {
            var $element = $(element);
            $element.click(function (event, confirm) {
                    if (!confirm) {
                        auiModule.instanceComponent(function ($component, $scope) {
                            $scope.body = attrs.aTitle;
                            $scope.confirm = function () {
                                $element.trigger("click", [true]);
                            };
                            $component.modal().show();
                        }, auiModule.tplSrc('confirm.html', attrs.aTitleTpl));

                        event.preventDefault();
                        event.stopPropagation();
                    }
                }
            );
        }
    };
});

